package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Build;
import com.xcity.db.entity.Player;
import com.xcity.game.build.BuildLevelUpTemplate;
import com.xcity.game.item.ItemInfo;
import com.xcity.pomelo.response.BuildListResponse.ItemReq;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class BuildLevelUpInfoResponse extends Response {

	private int upConsumeGold; // 升级消耗
	private LevelUpInfo upLevelInfo;
	private int leftCD; // 建造/升级剩余cd
	
	public static class LevelUpInfo {
		private int cdTime;
		private int price;
		private int health;
		private int maxIncome;
		private int income;
		private List<ItemReq> needItems;
		public int getCdTime() {
			return cdTime;
		}
		public void setCdTime(int cdTime) {
			this.cdTime = cdTime;
		}
		public int getPrice() {
			return price;
		}
		public void setPrice(int price) {
			this.price = price;
		}
		public int getHealth() {
			return health;
		}
		public void setHealth(int health) {
			this.health = health;
		}
		public int getMaxIncome() {
			return maxIncome;
		}
		public void setMaxIncome(int maxIncome) {
			this.maxIncome = maxIncome;
		}
		public int getIncome() {
			return income;
		}
		public void setIncome(int income) {
			this.income = income;
		}
		public List<ItemReq> getNeedItems() {
			return needItems;
		}
		public void setNeedItems(List<ItemReq> needItems) {
			this.needItems = needItems;
		}
	}
	
	public BuildLevelUpInfoResponse(int serial, Player player, Build build) {
		super(serial);
		BuildLevelUpTemplate template = build.getTemplate().getLevelUpTemplate(build.getLevel() + build.getTryAddLevel() + 1);
		LevelUpInfo info = new LevelUpInfo();
		info.cdTime = template.getCd() * 1000;
		info.price = template.getPrice();
		info.health = template.getRisk();
		info.maxIncome = template.getMaxIncome();
		info.income = template.getIncome();
		ItemInfo[] items = template.getNeedItems();
		if (items == null || items.length == 0) {
			info.needItems = Collections.emptyList();
		} else {
			info.needItems = new ArrayList<ItemReq>(items.length);
			for (ItemInfo item : items) {
				ItemReq req = new ItemReq();
				req.id = item.getId();
				req.count = item.getCount();
				req.name = item.getTemplate().getName();
				req.icon = item.getTemplate().getIcon();
				req.desc = item.getTemplate().getDesc();
				req.state = (byte) (player.getBag().hasEnough(req.id, req.count) ? 1 : 0);
				info.needItems.add(req);
			}
		}
		this.upLevelInfo = info;
		this.upConsumeGold = template.getCost();
		this.leftCD = build.getLeftCD();
	}

	public int getUpConsumeGold() {
		return upConsumeGold;
	}

	public void setUpConsumeGold(int upConsumeGold) {
		this.upConsumeGold = upConsumeGold;
	}

	public LevelUpInfo getUpLevelInfo() {
		return upLevelInfo;
	}

	public void setUpLevelInfo(LevelUpInfo upLevelInfo) {
		this.upLevelInfo = upLevelInfo;
	}

	public int getLeftCD() {
		return leftCD;
	}

	public void setLeftCD(int leftCD) {
		this.leftCD = leftCD;
	}
}
